package com.mall.db.api;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.mapper.SmsHomeAdvertiseMapper;
import com.mall.domain.SmsHomeAdvertise;
import com.mall.interface1.api.SmsHomeAdvertiseServiceApi;
import com.mall.vo.SmsHomePageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class SmsHomeAdvertiseServiceApiImpl implements SmsHomeAdvertiseServiceApi {

    @Autowired
    SmsHomeAdvertiseMapper smsHomeAdvertiseMapper;


    @Override
    public SmsHomePageBeanVo findByPage(String endTime, String name, Integer pageNum, Integer pageSize, Integer type) {
        Page<SmsHomeAdvertise> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsHomeAdvertise> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotEmpty(name),SmsHomeAdvertise::getName,name);
        qw.like(StrUtil.isNotEmpty(endTime),SmsHomeAdvertise::getEndTime,endTime);
        qw.eq(type != null, SmsHomeAdvertise::getType, type);
        qw.orderByDesc(SmsHomeAdvertise::getSort);

        Page<SmsHomeAdvertise> smsHomeAdvertisePage = smsHomeAdvertiseMapper.selectPage(page, qw);
        return new SmsHomePageBeanVo(pageNum,pageSize,smsHomeAdvertisePage.getTotal(),smsHomeAdvertisePage.getRecords());
    }


    @Override
    public void addCreate(SmsHomeAdvertise smsHomeAdvertise) {
        smsHomeAdvertiseMapper.insert(smsHomeAdvertise);
    }

    @Override
    public void delete(Long id) {
        smsHomeAdvertiseMapper.deleteById(id);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        SmsHomeAdvertise smsHomeAdvertise = smsHomeAdvertiseMapper.selectById(id);
        smsHomeAdvertise.setStatus(status);
        smsHomeAdvertiseMapper.updateById(smsHomeAdvertise);
    }

    @Override
    public void updateAdvertise(SmsHomeAdvertise advertise, Long id) {
        smsHomeAdvertiseMapper.updateById(advertise);
    }

    @Override
    public SmsHomeAdvertise findById(Long id) {
        return smsHomeAdvertiseMapper.selectById(id);
    }


}
